package models

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"log"
)

type EmployeeInfo struct {
	Id           int    `json:"id" form:"id"`
	Name         string `json:"name" form:"name"`
	Status       int    `json:"status" form:"status"`
	ArriveDate   string `json:"arriveDate" form:"arriveDate"`
	Job          string `json:"job" form:"job"`
	Position     string `json:"position" form:"workPlace"`
	DepartmentID int    `json:"departmentId" form:"depId"`
}

type DepartmentInfo struct {
	DepartmentID int    `json:"departmentId" form:"depId"`
	Name         string `json:"name" form:"name"`
}

type TransferInfo struct {
	Id        int    `json:"id" form:"id"`
	WorkId    int    `json:"workId" form:"workId"`
	TransDate string `json:"transDate" form:"transDate"`
	InDep     int    `json:"inDep" form:"inDep"`
	OutDep    int    `json:"outDep" form:"outDep"`
	Why       string `json:"why" form:"why"`
}

var Db *sql.DB
var getPasswordFromDbQuery *sql.Stmt
var createNewEmpQuery *sql.Stmt
var createNewDepQuery *sql.Stmt
var updateEmp *sql.Stmt
var createTransInfo *sql.Stmt
var transEmpQuery *sql.Stmt

func init() {

	var err error
	Db, err = sql.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/worker_transfer_informations")
	CheekErr(err)
	getPasswordFromDbQuery, err = Db.Prepare("SELECT password FROM worker_transfer_informations.password where username=?")
	CheekErr(err)
	createNewEmpQuery, err = Db.Prepare("INSERT INTO worker_transfer_informations.employees (Name, Status, ArriveDate, Job, Position, DepartmentID) VALUE (?,?,?,?,?,?)")
	CheekErr(err)
	createNewDepQuery, err = Db.Prepare("INSERT INTO worker_transfer_informations.departments (Name) value (?)")
	CheekErr(err)
	updateEmp, err = Db.Prepare("UPDATE worker_transfer_informations.employees SET Name=? ,Status=?,ArriveDate=?,Job=?,Position=? WHERE WorkerID=?")
	CheekErr(err)
	createTransInfo,err=Db.Prepare("INSERT INTO worker_transfer_informations.transfer_informations (WorkID, TransDate, InDepartment, OutDepartment, Why) value (?,?,?,?,?)")
	CheekErr(err)
	transEmpQuery, err = Db.Prepare("UPDATE worker_transfer_informations.employees SET Name=? ,Status=?,ArriveDate=?,Job=?,Position=?,DepartmentID=? WHERE WorkerID=?")
	CheekErr(err)
}

func CheekErr(e error) {
	if e != nil {
		log.Fatal(e)
	}
}
